// 新碟 nav
myajax('http://47.97.121.9:3300/getNewDisks')
    .then(res => {
    
        let nav = res.response.new_album_tag.data.area;
        renderNav(nav);
        $('.album_nav>ul>li').eq(0).children().addClass('ac');
    })

// 渲染新碟 nav
function renderNav(data) {
    data.forEach((v, k) => {
        $('.album_nav>ul').append(`
             <li><a href="javascript:;" data-id="${v.id}">${v.name}</a></li>
        `);
    })
}

// 渲染新碟列表
function renderAlbum(data) {
    $('.album_item').remove();
    data.forEach((v, k) => {
        $('.album_list').append(`
            <li class="album_item">
                <div class="box">
                    <a href="javascript:;" class="album_img">
                        <img src="https://y.qq.com/music/photo_new/T002R300x300M000${v.mid}.jpg?max_age=2592000" alt="">
                        <i class="icon_play"></i>
                    </a>
                </div>
                <div class="album_info">
                    <a href="javascript:;" class="album_name">${v.name}</a>
                    <a href="javascript:;" class="album_author">${v.singers[0].name}</a>
                    <span class="album_time">${v.release_time}</span>
                </div>
            </li>
        `);
    })
}

$(document).on('click', '.album_nav>ul>li>a', function () {
    $(this).addClass('ac').parent().siblings().children().removeClass('ac');
    let navId = $(this).attr('data-id');
    myajax(`http://47.97.121.9:3300/getNewDisks?page=${navId}&limit=20`)
    .then(res => {
        let item = res.response.new_album.data.albums;
        renderAlbum(item);
    })
})


load();
function load() {
    //滚动加载
    var stop = true;
    var start = 0;
    $.ajaxSetup({ async: false });
    function getData(url) {
        $.ajax({
            type: "get",//跨域访问时只能用get
            url: url,//要访问的api地址
            dataType: "json",
            success: function (data) {
                let item = data.response.new_album.data.albums;
                item.forEach((v, k) => {
                    $('.album_list').append(`
                        <li class="album_item">
                            <div class="box">
                                <a href="javascript:;" class="album_img">
                                    <img src="https://y.qq.com/music/photo_new/T002R300x300M000${v.mid}.jpg?max_age=2592000" alt="">
                                    <i class="icon_play"></i>
                                </a>
                            </div>
                            <div class="album_info">
                                <a href="javascript:;" class="album_name">${v.name}</a>
                                <a href="javascript:;" class="album_author">${v.singers[0].name}</a>
                                <span class="album_time">${v.release_time}</span>
                            </div>
                        </li>
                    `);
                })
                stop = true;
            },
        });
    }
    getData('http://47.97.121.9:3300/getNewDisks');
    $(window).scroll(function () {
        var url = "http://47.97.121.9:3300/getNewDisks?page=" + start + "&limit=20";
        var totalheight = parseFloat($(window).height()) + parseFloat($(window).scrollTop());
        if ($(document).height() <= totalheight + 120) {
            if (stop == true) {
                start++;
                stop = false;
                $('#j_loadmore').addClass('enabled');
                getData(url);
            }
        }
    })
}